package com.yanqu.road.server.manager.activity.daomu.pvp.task;

import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.manager.activity.daomu.DaoMuActivity;
import com.yanqu.road.server.manager.activity.daomu.DaoMuActivityMgr;
import com.yanqu.road.server.manager.activity.daomu.pvp.DaoMuActivityPvp;
import com.yanqu.road.utils.date.DateHelper;
import com.yanqu.road.utils.executor.GeneralTask;

import java.util.Date;
import java.util.Map;

/**
 * npc行为
 */
public class DaoMuPvpNpcTask extends GeneralTask {

    private long npcMoveTaskTime;

    private long npcActionTaskTime;

    // 怪物创建
    private long createMonsterTime;

    // 怪物移动
    private long monsterMoveTaskTime;

    // 怪物寻找目标
    private long monsterFindUserTaskTime;

    @Override
    public void doRun() {
        Map<Integer, DaoMuActivity> activityMap = DaoMuActivityMgr.getActivityMap();
        if (activityMap == null || activityMap.isEmpty()) {
            // 无活动
            return;
        }

        // 移动 次/0.1s
        if (System.currentTimeMillis() > npcMoveTaskTime) {
            npcMoveTaskTime = System.currentTimeMillis() + 100;
            for (DaoMuActivity daoMuActivity : activityMap.values()) {
                try {
                    if (!ActivityHelper.activityInTime(daoMuActivity.getDaoMuActivityConfig().getActivityInfo())) {
                        continue;
                    }
                    DaoMuActivityPvp activityPvp = daoMuActivity.getDaoMuActivityPvp();
                    if (activityPvp != null) {
                        activityPvp.daoMuPvpNpcMoveTask();
                    }
                } catch (Exception e) {
                    getLOGGER().error("daoMuPvpNpcMoveTask error : ", e);
                }
            }
        }

        // npc行为 次/1s
        if (System.currentTimeMillis() > npcActionTaskTime) {
            npcActionTaskTime = System.currentTimeMillis() + 1000;
            for (DaoMuActivity daoMuActivity : activityMap.values()) {
                try {
                    if (!ActivityHelper.activityInTime(daoMuActivity.getDaoMuActivityConfig().getActivityInfo())) {
                        continue;
                    }
                    DaoMuActivityPvp activityPvp = daoMuActivity.getDaoMuActivityPvp();
                    if (activityPvp != null) {
                        activityPvp.daoMuPvpNpcActionTask();
                    }
                } catch (Exception e) {
                    getLOGGER().error("daoMuPvpNpcActionTask error : ", e);
                }
            }
        }


        // 怪物 次/1s
        if (System.currentTimeMillis() > createMonsterTime) {
            createMonsterTime = System.currentTimeMillis() + 1000;
            for (DaoMuActivity daoMuActivity : activityMap.values()) {
                try {
                    DaoMuActivityPvp activityPvp = daoMuActivity.getDaoMuActivityPvp();
                    if (activityPvp != null) {
                        activityPvp.daoMuPvpCreateMonsterTask();
                    }
                } catch (Exception e) {
                    getLOGGER().error("daoMuPvpCreateMonsterTask error : ", e);
                }
            }
        }

        // 怪物 次/1s
        if (System.currentTimeMillis() > monsterMoveTaskTime) {
            monsterMoveTaskTime = System.currentTimeMillis() + 100;
            for (DaoMuActivity daoMuActivity : activityMap.values()) {
                try {
                    DaoMuActivityPvp activityPvp = daoMuActivity.getDaoMuActivityPvp();
                    if (activityPvp != null) {
                        activityPvp.daoMuPvpMonsterMoveTask();
                    }
                } catch (Exception e) {
                    getLOGGER().error("daoMuPvpMonsterMoveTask error : ", e);
                }
            }
        }


        // 怪物 次/10s
        if (System.currentTimeMillis() > monsterFindUserTaskTime) {
            monsterFindUserTaskTime = System.currentTimeMillis() + DateHelper.SECOND_MILLIONS;
            for (DaoMuActivity daoMuActivity : activityMap.values()) {
                try {
                    DaoMuActivityPvp activityPvp = daoMuActivity.getDaoMuActivityPvp();
                    if (activityPvp != null) {
                        activityPvp.daoMuPvpMonsterLockTask();
                    }
                } catch (Exception e) {
                    getLOGGER().error("daoMuPvpMonsterLockTask error : ", e);
                }
            }
        }


    }
}
